package com.wtwd.device.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.dao.AppHealthTypeMapper;
import com.wtwd.device.model.entity.ExcelImportLanguage;
import com.wtwd.device.model.entity.HealthType;
import com.wtwd.device.service.AppFileService;
import com.wtwd.device.service.AppHealthTypeService;
import com.wtwd.device.service.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2023-08-16
 */
@Service
public class AppHealthTypeServiceImpl extends ServiceImpl<AppHealthTypeMapper, HealthType> implements AppHealthTypeService {


    @Value("${healthType.excel}")
    private String healthTypePath;

    @Value("${file.path}")
    private String filePath;

    @Value("${file.http}")
    private String fileHttp;

    @Value("${healthContent.excel}")
    private String healthContentPath;

    @Value("${healthType.images}")
    private String healthTypeImages;

    @Resource
    private AppHealthTypeMapper healthTypeMapper;

    @Resource
    private RedisService redisService;

    @Resource
    private AppFileService appFileService;


    @Override
    public List<HealthType> queryPage(Integer pageNo, Integer pageSize, String languageType) throws Exception {
        Integer start = pageNo * pageSize;
        List<HealthType> healthTypes = healthTypeMapper.queryPage(start, pageSize);
        if (healthTypes!=null&&healthTypes.size()>0){
            healthTypes.stream().forEach(e->{
                if (StringUtils.isNotBlank(e.getImage())){
                    e.setImage(fileHttp.concat(healthTypeImages).concat(e.getImage()));
                }
            });
        }
        return translatorsTypeLanguage(healthTypes, languageType);
    }

    @Override
    public List<HealthType> translatorsTypeLanguage(List<HealthType> healthTypes, String languageType) throws Exception {
        if (UNTRANSLATED_LANGUAGE.equals(languageType)) {//如果是中文不进行翻译
            return healthTypes;
        }
        if (healthTypes != null && healthTypes.size() != 0) {
            Map<String, Object> cache = redisService.getHashValue(HEATH_TYPE_KEY_PREFIX);
            for (HealthType healthType : healthTypes) {
                String cacheLanguage = (String) cache.get(healthType.getId().toString());
                if (StringUtils.isNotBlank(cacheLanguage)) {//缓存文件的数据不为空,命中缓存开始翻译语言
                    List<ExcelImportLanguage> languagesData = JSONArray.parseArray((String) cache.get(healthType.getId().toString()), ExcelImportLanguage.class);
                    List<ExcelImportLanguage> collect = languagesData.stream().filter(e -> e.getType().equals(languageType)).collect(Collectors.toList());
                    ExcelImportLanguage language = null;
                    if (collect != null && collect.size() > 0) {
                        language = collect.get(0);
                    }
                    if (language != null && StringUtils.isNotBlank(language.getTranslation())) {
                        healthType.setName(language.getTranslation());
                    } else {//如果语言类型不存在则默认查询英语
                        ExcelImportLanguage defaultLanguage = null;
                        List<ExcelImportLanguage> collect1 = languagesData.stream().filter(e -> e.getType().equals(DEFAULT_LANGUAGE_TYPE)).collect(Collectors.toList());
                        if (collect1 != null && collect1.size() > 0) {
                            defaultLanguage = collect1.get(0);
                        }
                        if (defaultLanguage != null && StringUtils.isNotBlank(defaultLanguage.getTranslation())) {
                            healthType.setName(defaultLanguage.getTranslation());
                        }
                    }
                } else {//为空则命中文件,文件如果存在且命中,即添加到缓存
                    if (StringUtils.isNotBlank(healthType.getPath())) {
                        List<ExcelImportLanguage> list = appFileService.readExcel(filePath.concat(healthTypePath).concat(healthType.getPath()));
                        if (list != null && list.size() > 0) {
                            list = list.stream().filter(e -> StringUtils.isNotBlank(e.getTranslation())).collect(Collectors.toList());
                        }
                        if (list != null && list.size() > 0) {
                            ExcelImportLanguage language = null;
                            List<ExcelImportLanguage> collect = list.stream().filter(e -> e.getType().equals(languageType)).collect(Collectors.toList());
                            if (collect != null && collect.size() > 0) {
                                language = collect.get(0);
                            } else {//如果文件中没有改语言类型，则寻找默认语言
                                List<ExcelImportLanguage> collect1 = list.stream().filter(e -> e.getType().equals(DEFAULT_LANGUAGE_TYPE)).collect(Collectors.toList());
                                if (collect1 != null && collect1.size() > 0) {
                                    language = collect1.get(0);
                                }
                            }
                            if (language != null && StringUtils.isNotBlank(language.getTranslation())) {
                                healthType.setName(language.getTranslation());
                            }
                            redisService.saveCache(HEATH_TYPE_KEY_PREFIX, healthType.getId().toString(), JSONObject.toJSONString(list));
                        }
                    }
                }
            }
        }
        return healthTypes;
    }

}
